package com.zw.cloud.mybatis.plus.util;

import com.zw.cloud.common.utils.BigDecimalUtils;
import com.zw.cloud.mybatis.plus.entity.stenter.ProductAssistantDTO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class ProductAssistantCompareUtils {

    public static void main(String[] args) {

    }
    /**
     * 助剂是否相同
     */
    public static boolean compareAssistant(List<ProductAssistantDTO> assistantDTOList, List<ProductAssistantDTO> craftAssistantDTOList) {
        if (CollectionUtils.isEmpty(assistantDTOList) && CollectionUtils.isEmpty(craftAssistantDTOList)) {
            return true;
        }
        if (CollectionUtils.isEmpty(assistantDTOList) && CollectionUtils.isNotEmpty(craftAssistantDTOList)) {
            return false;
        }
        if (CollectionUtils.isNotEmpty(assistantDTOList) && CollectionUtils.isEmpty(craftAssistantDTOList)) {
            return false;
        }
        // 都不为空
        if (assistantDTOList.size() != craftAssistantDTOList.size()) {
            return false;
        }
        Set<Long> idSet = assistantDTOList.stream().map(ProductAssistantDTO::getId).collect(Collectors.toSet());
        Set<Long> craftIdSet = craftAssistantDTOList.stream().map(ProductAssistantDTO::getId).collect(Collectors.toSet());
        if (!idSet.containsAll(craftIdSet) || !craftIdSet.containsAll(idSet)) {
            return false;
        }
        Map<Long, ProductAssistantDTO> craftAssistantMap = craftAssistantDTOList.stream().collect(Collectors.toMap(ProductAssistantDTO::getId, v -> v, (k1, k2) -> k2));
        for (ProductAssistantDTO productAssistantDTO : assistantDTOList) {
            ProductAssistantDTO craftAssistantDTO = craftAssistantMap.get(productAssistantDTO.getId());
            boolean equaulsBigDecimal = BigDecimalUtils.equalsBigDecimal(productAssistantDTO.getValue(), craftAssistantDTO.getValue());
            if (!equaulsBigDecimal) {
                return false;
            }
            boolean equauls = BigDecimalUtils.equalsBigDecimal(productAssistantDTO.getDosage(), craftAssistantDTO.getDosage());
            if (!equauls) {
                return false;
            }
        }
        return true;
    }

}
